package com.meilai.project.mapper.business.installOrder;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meilai.project.dto.app.WorkerQueryWorkOrderDTO;
import com.meilai.project.dto.business.installOrder.WorkOrderSearchDTO;
import com.meilai.project.dto.business.installOrder.WorkerDTO;
import com.meilai.project.entity.business.installOrder.WorkOrder;
import com.meilai.project.entity.media.MediaPointExt;
import com.meilai.project.vo.app.*;
import com.meilai.project.vo.business.installOrder.*;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@Mapper
public interface WorkOrderMapper extends BaseMapper<WorkOrder> {

    List<MediaPointExt> listByWorkOrderId(@Param("work_order_id_list") List<Long> workOrderId);

    void updateMode(@Param("chooseIdList") List<Long> chooseIdList, @Param("mode") Integer mode);


    @Select("select bs.id as bill_sale_id, wo.id as work_order_id,  " +
            "    wo.`name` as work_order_name,mp.media_type_id, mt.`name` as media_type_name, " +
            "count(ws.media_point_id) as publish_count  " +
            "from worker_submit ws  " +
            "join work_order wo on wo.id = ws.work_order_id  " +
            "join bill_sale bs on wo.bill_sale_id = bs.id  " +
            "join contract_apply ca on bs.contract_id = ca.id  " +
            "join media_point mp on ws.media_point_id = mp.id " +
            "JOIN media_type mt on mp.media_type_id = mt.id " +
            " where ca.id = #{contract_id}  " +
            "group by wo.id, mp.media_type_id")
    List<WorkerOrderPointPublishVO> queryPublishInfoByContractId(@Param("contract_id") Long contractId);

    @Select("select bs.id as bill_sale_id, wo.id as work_order_id, " +
            "mp.media_type_id, mt.`name` as media_type_nname, " +
            "count(mpcdm.media_point_id) as total_count  " +
            "from bill_sale bs   " +
            "join work_order wo on wo.bill_sale_id = bs.id  " +
            "join contract_apply ca on bs.contract_id = ca.id   " +
            "join media_point_choose_daterange_mediapoint mpcdm on mpcdm.choose_id = bs.choose_id   " +
            "join media_point mp on mpcdm.media_point_id = mp.id " +
            "JOIN media_type mt on mp.media_type_id = mt.id " +
            "  where ca.id = #{contract_id}   " +
            "group by bs.id, mp.media_type_id")
    List<WorkerOrderPointPublishVO> queryWorkOrderTotalCount(@Param("contract_id") Long contractId);

    List<ProcessingWorkOrder5Ids> getWorkerProcessingWorkOrder5Ids(@Param("workerId") Long workerId);

    @Select("SELECT " +
            "(SELECT COUNT(1) FROM worker_submit a WHERE a.worker_id=#{workerId} AND a.deleted_at IS NULL) AS submit_count " +
            ", (SELECT count(1) FROM v_work_order_cur_status a WHERE a.id IN (SELECT DISTINCT b.work_order_id FROM work_order_install_detail_worker b WHERE b.worker_id=#{workerId}) AND a.closed=0 AND a.expired=0) AS work_order_count_processing " +
            ", (SELECT count(1) FROM v_work_order_cur_status a WHERE a.id IN (SELECT DISTINCT b.work_order_id FROM worker_submit b WHERE b.deleted_at IS NULL AND b.worker_id=#{workerId}) AND a.closed=1) AS work_order_count_finished " +
            ", (SELECT count(1) FROM v_work_order_cur_status a WHERE a.id IN (SELECT DISTINCT b.work_order_id FROM work_order_install_detail_worker b WHERE b.worker_id=#{workerId}) AND a.work_order_type_id=1 AND a.closed=0 AND a.expired=0) AS work_order_count_processing_install " +
            ", (SELECT count(1) FROM v_work_order_cur_status a WHERE a.id IN (SELECT DISTINCT b.work_order_id FROM work_order_install_detail_worker b WHERE b.worker_id=#{workerId}) AND a.work_order_type_id=2 AND a.closed=0 AND a.expired=0) AS work_order_count_processing_publish " +
            ", (SELECT count(1) FROM v_work_order_cur_status a WHERE a.id IN (SELECT DISTINCT b.work_order_id FROM work_order_install_detail_worker b WHERE b.worker_id=#{workerId}) AND a.work_order_type_id=3 AND a.closed=0 AND a.expired=0) AS work_order_count_processing_unpublish " +
            ", (SELECT count(1) FROM v_work_order_cur_status a WHERE a.id IN (SELECT DISTINCT b.work_order_id FROM work_order_install_detail_worker b WHERE b.worker_id=#{workerId}) AND a.work_order_type_id=4 AND a.closed=0 AND a.expired=0) AS work_order_count_processing_check " +
            ", (SELECT count(1) FROM v_work_order_cur_status a WHERE a.id IN (SELECT DISTINCT b.work_order_id FROM work_order_install_detail_worker b WHERE b.worker_id=#{workerId}) AND a.work_order_type_id=5 AND a.closed=0 AND a.expired=0) AS work_order_count_processing_repair")
    WorkerIndexVO getAppIndexCountInfo(@Param("workerId") Long workerId);

    @Select("SELECT  " +
            "wo.id " +
            ", wo.`name` " +
            ", wo.work_order_type_id " +
            ", a.total_num " +
            ", a.installed_to_check_num AS submit_to_check_num " +
            ", a.installed_passed_num AS submit_passed_num " +
            ", a.installed_total_num AS submit_total_num " +
            ", a.expired AS is_expired " +
            ", a.closed AS is_finished " +
            ", wo.created_at " +
            ", b.newest_worker_submit " +
            "FROM work_order wo " +
            "JOIN v_work_order_cur_status a ON a.installed_total_num>0 AND a.closed=0 AND wo.id=a.id " +
            "LEFT JOIN " +
            "(SELECT x.work_order_id,MAX(x.created_at) AS newest_worker_submit " +
            "FROM worker_submit x GROUP BY x.work_order_id " +
            ") b ON wo.id=b.work_order_id " +
            "WHERE wo.deleted_at IS NULL AND wo.`status`=1 " +
            "ORDER BY wo.created_at DESC " +
            "LIMIT 3")
    List<WorkerIndexWorkOrderProcessingVO> getAppIndexProcessingWorkOrders();

    List<WorkerQueryWorkOrderVO> selectPageListForApp(Page<WorkerQueryWorkOrderVO> page,
                                                      @Param("searchDTO") WorkerQueryWorkOrderDTO dto);

    List<WorkerVO> selectWorker(@Param("searchDTO") WorkerDTO workerDTO);

    List<WorkOrderVO> selectPageList(Page<WorkOrderVO> page, @Param("searchDTO") WorkOrderSearchDTO searchDTO);

    List<Long> getWorkIdsByMediaTypeIds(@Param("ids") List<Long> ids);

    List<WorkOrderMediaTypeVO> getWorkMediaTypeList(@Param("ids") List<Long> ids);

    @Select(" select * from work_order wo " +
            " where wo.deleted_at is null and wo.deadline > NOW() " +
            " and work_order_type_id = #{work_order_type_id} " +
            " and id in ( " +
            " select id from v_work_order_cur_status " +
            " where work_order_type_id = #{work_order_type_id} and mode = 0 " +
            ") ")
    List<WorkOrder> selectForSelectOptions(@Param("work_order_type_id") Integer work_order_type_id);

    @Select("SELECT installed_passed_num FROM v_work_order_nums WHERE id=#{work_order_id}")
    Integer getWorkOrderPassedNum(@Param("work_order_id") Long work_order_id);

    Integer getWorkOrderPassedNumByIds(@Param("ids") List<Long> ids);

    //    @Select("SELECT  " +
//            "d.work_order_id " +
//            ",d.daterange " +
//            ",d.building_area_id " +
//            ",d.building_area " +
//            ",d.building_area_address " +
//            ",d.point_type_id " +
//            ",d.point_type " +
//            ",COUNT(1) AS `count`  " +
//            "FROM v_work_order_passed_for_report d  " +
//            "WHERE d.work_order_id=#{work_order_id} " +
//            "GROUP BY d.work_order_id,d.daterange,d.building_area_id,d.point_type_id " +
//            "ORDER BY d.building_area_id,d.point_type_id")
    List<WorkOrderReportDetailByBuildingVO> getWorkOrderReportBuildingVOs(@Param("work_order_id") Long work_order_id);


    List<WorkOrderReportDetailByBuildingVO> getWorkOrderReportBuildingVOsByIds(@Param("ids") List<Long> ids);

    //    @Select("<script>" +
//            "SELECT  " +
//            "a.id " +
//            ",a.code " +
//            ",a.daterange " +
//            ",a.building_area_id " +
//            ",a.building_area " +
//            ",a.building_area_photo " +
//            ",a.building_area_address " +
//            ",c.pid AS point_type_pid " +
//            ",a.point_type_id " +
//            ",a.building_number " +
//            ",a.unit_number " +
//            ",a.floor " +
//            ",a.door_number " +
//            ",a.is_light " +
//            ",b.url " +
//            "FROM  " +
//            "v_work_order_passed_for_report a  " +
//            "LEFT JOIN  " +
//            "<choose> " +
//            " <when test=\"is_first_img==true\"> " +
//            "   (SELECT id, worker_submit_id, url, upload_at FROM worker_submit_photo WHERE id IN (SELECT ids FROM v_work_order_first_img_for_report))  " +
//            " </when> " +
//            " <otherwise> " +
//            "   worker_submit_photo  " +
//            " </otherwise> " +
//            "</choose> " +
//            "b ON a.id=b.worker_submit_id " +
//            "LEFT JOIN dictionary c ON a.point_type_id=c.id " +
//            "WHERE work_order_id=#{work_order_id} " +
//            "ORDER BY a.building_area_id,a.daterange,a.id,b.upload_at" +
//            "</script>")

    /**
     * 获取工单的图片
     *
     * @param work_order_id
     * @param is_first_img
     * @return
     */
    List<WorkOrderReportPhotoVO> getPointPhotos(@Param("work_order_id") Long work_order_id, @Param("is_first_img") Boolean is_first_img);

    /**
     * 获取多工单的图片
     *
     * @param ids
     * @param is_first_img
     * @return
     */
    List<WorkOrderReportPhotoVO> getPointPhotosByIds(@Param("ids") List<Long> ids, @Param("is_first_img") Boolean is_first_img);

    List<WorkOrderProcessOfBuildingVO> listProcessOfBuilding(@Param("work_order_id") Long work_order_id, @Param("name") String name);

    Map<String, Integer> getAllWorkOrderProgressByTypeId(@Param("work_order_type_id") Long workOrderTypeId);

    List<SubmissionDetailVO> getWorkOrderSubmitted(@Param("page") Page<SubmissionDetailVO> page, @Param("work_order_id") Long work_order_id, @Param("building_id") Long building_id, @Param("code") String code);

    List<SubmissionDetailVO> getWorkOrderSubmitted2(@Param("page") Page<SubmissionDetailVO> page, @Param("work_order_id") Long work_order_id, @Param("building_id") Long building_id, @Param("code") String code);

    List<SubmissionDetailVO> getWorkOrderNotSubmittedByBuildingRelevant(@Param("page") Page<SubmissionDetailVO> page, @Param("work_order_id") Long work_order_id, @Param("building_id") Long building_id, @Param("code") String code);

    List<SubmissionDetailVO> getWorkOrderNotSubmittedBySaleOrderRelevant(@Param("page") Page<SubmissionDetailVO> page, @Param("work_order_id") Long work_order_id, @Param("building_id") Long building_id, @Param("code") String code);

    List<WorkerQueryWorkOrderVO> getWorkOrderList(Page<WorkerQueryWorkOrderVO> page, @Param("searchDTO") WorkerQueryWorkOrderDTO dto, @Param("worker_id") Long worker_id);

    /**
     * 根据工单Id获取新安装类型工单未安装设备个数
     *
     * @param orderId
     * @return
     */
    List<WorkMediaPointNoInstallVO> getDeviceNoInstallByOrderId(@Param("orderId") Long orderId, @Param("building_area_id") Long building_area_id);

    /**
     * 获取工单已提交设备详情
     *
     * @param orderId
     * @return
     */
    List<WorkMediaPointDetailVO> getOrderSubmittedDeviceDetail(@Param("orderId") Long orderId, @Param("code") String code, @Param("building_area_id") Long building_area_id, @Param("worker_id") Long worker_id, @Param("work_order_type_id") Integer work_order_type_id, @Param(value = "page") Page<WorkMediaPointDetailVO> page);

    /**
     * 获取工单未提交设备信息
     *
     * @param orderId
     * @return
     */
    List<WorkMediaPointDetailVO> getOrderNotSubmittedDeviceDetail(@Param("orderId") Long orderId, @Param("code") String code, @Param("building_area_id") Long building_area_id, @Param("worker_id") Long worker_id, @Param(value = "page") Page<WorkMediaPointDetailVO> page);

    List<JSONObject> getBuildingByBuildIdAndOrderIdAndOrderType(@Param("building_area_id") Long building_area_id, @Param("work_order_id") Long work_order_id, @Param("work_order_type_id") Integer work_order_type_id, @Param("worker_id") Long worker_id, @Param("building_name") String building_name, @Param("has_customer") String has_customer, @Param("ids") List<Long> ids);

    //List<JSONObject> getBuildingByBuildIdAndOrderIdAndOrderType(@Param("building_area_id") Long building_area_id, @Param("work_order_id") Long work_order_id, @Param("work_order_type_id") Integer work_order_type_id, @Param("worker_id") Long worker_id, @Param("building_name") String building_name, @Param("has_customer") String has_customer);

    List<JSONObject> getBuildingDeviceList(Page<JSONObject> page, @Param("building_area_id") Long building_area_id, @Param("device_code") String device_code);

    List<JSONObject> getBuildingOrderList(@Param("building_area_id") Long building_area_id, @Param("device_id") Long device_id);

    List<JSONObject> getOrderByDevice(@Param("device_id") Long device_id);

    /**
     * 获取维修工单总数量-只统计进行中和未执行的
     */
    @Select("select vs.`id`,vs.`work_order_type_id` from `v_work_order_cur_status` vs where vs.`work_order_type_id`=5 and (vs.`unexcute`=1 or vs.`executing`=1)")
    List<WorkOrder> listWorkOrderNumOfRepair();

    /**
     * 获取除维修工单外其他类型总数量-只统计进行中和未执行的
     */
    @Select("select a.* from `work_order` a left join `v_work_order_cur_status` b on a.`id`=b.`id` where ISNULL(a.`deleted_at`) and a.`status`=1 and b.`closed` = 0 and b.`hand_closed`=0 and b.`expired`=0 and (b.`unexcute`=1 or b.`executing`=1)")
    List<WorkOrder> selectListWorkOrderNumOfType();

    List<WorkOrderVO> selectListVoByIds(@Param("ids") List<Long> ids);

    List<WorkOrderPointDetailVO> getWorkOrderPointDetailList(@Param("id") Long id);

    List<WorkOrderPointDetailVO> getCount(@Param("id") Long id);

    List<WorkOrderPointDetailVO> getPointDetailListByLockId(@Param("lockId") Long lockId);

    List<WorkOrderPointDetailVO> getPointDetailListCountByLockId(@Param("lockId") Long lockId);

    @Delete("delete from work_order_id_range")
    void clearWorkOrderIdRange();

    //新安装、巡检、维修、监播
    void generateWorkOrderTypeOf1458(@Param("beginDate") LocalDate beginDate, @Param("endDate") LocalDate endDate, @Param("type") Integer type);

    //换画
    void generateWorkOrderTypeOf7(@Param("beginDate") LocalDate beginDate, @Param("endDate") LocalDate endDate, @Param("type") Integer type);

    //上刊
    void generateWorkOrderTypeOf2(@Param("beginDate") LocalDate beginDate, @Param("endDate") LocalDate endDate, @Param("type") Integer type);

    //下刊
    void generateWorkOrderTypeOf3(@Param("beginDate") LocalDate beginDate, @Param("endDate") LocalDate endDate, @Param("type") Integer type);

    List<Long> getBuildingByBuildIdAndOrderId(@Param("page") Page<JSONObject> pageObj, @Param("building_area_id") Long building_area_id, @Param("work_order_id") Long work_order_id, @Param("work_order_type_id") Integer work_order_type_id, @Param("worker_id") Long worker_id, @Param("building_name") String building_name, @Param("has_customer") String has_customer);
}

